#include "kn_daemonize.h"
#include "sys/wait.h"

static char LOCKFILE[256] = "/var/run/daemon.pid";
static char WORKDIR[256] = "/";

void daemonize()
{
	int	i, fd0, fd1, fd2;
	pid_t	pid;
	struct rlimit	rl;
	struct sigaction	sa;

	/*
	 * Clear file creation mask.
	 */
	umask(0);

	/*
	 * Get maximum number of file descriptors.
	 */
	if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
		exit(0);
		//err_quit("%s: can't get file limit", cmd);

	/*
	 * Become a session leader to lose controlling TTY.
	 */
	if ((pid = fork()) < 0)
		exit(0);
		//err_quit("%s: can't fork", cmd);
	else if (pid != 0) /* parent */{
		wait(NULL);
		exit(0);
	}
	setsid();
	/*
	 * Ensure future opens won't allocate controlling TTYs.
	 */
	sa.sa_handler = SIG_IGN;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;
	if (sigaction(SIGHUP, &sa, NULL) < 0)
		exit(0);//err_quit("%s: can't ignore SIGHUP");
	if ((pid = fork()) < 0)
		exit(0);//err_quit("%s: can't fork", cmd);
	else if (pid != 0) /* parent */
		exit(0);	
	/*
	 * Change the current working directory to the root so
	 * we won't prevent file systems from being unmounted.
	 */
	//if (chdir(WORKDIR) < 0){
	//	printf(" can't change directory to %s\n",WORKDIR);
	//	exit(0);//err_quit("%s: can't change directory to /");
	//}
	/*
	 * Close all open file descriptors.
	 */
	if (rl.rlim_max == RLIM_INFINITY)
		rl.rlim_max = 1024;
	for (i = 0; i < rl.rlim_max; i++)
		close(i);
	/*
	 * Attach file descriptors 0, 1, and 2 to /dev/null.
	 */
	fd0 = open("/dev/null", O_RDWR);
	fd1 = dup(0);
	fd2 = dup(0);
	/*
	 * Initialize the log file.
	 */
	//openlog(cmd, LOG_CONS, LOG_DAEMON);
	if (fd0 != 0 || fd1 != 1 || fd2 != 2) {
		syslog(LOG_ERR, "unexpected file descriptors %d %d %d",fd0, fd1, fd2);
		exit(1);
	}
}

int
lockfile(int fd)
{
	struct flock fl;

	fl.l_type = F_WRLCK;
	fl.l_start = 0;
	fl.l_whence = SEEK_SET;
	fl.l_len = 0;
	return(fcntl(fd, F_SETLK, &fl));
}

#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)

int already_running(void)
{
	int	fd;
	char	buf[16];
	fd = open(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE);
	if (fd < 0) {
		syslog(LOG_ERR, "can't lock %s: %s", LOCKFILE, strerror(errno));
		exit(1);
	}
	if (lockfile(fd) < 0) {
		if (errno == EACCES || errno == EAGAIN) {
			close(fd);
			return(1);
		}
		syslog(LOG_ERR, "can't lock %s: %s", LOCKFILE, strerror(errno));
		exit(1);
	}
	ftruncate(fd, 0);
	sprintf(buf, "%ld", (long)getpid());
	write(fd, buf, strlen(buf)+1);
	return 0;
}

void set_lockfile(const char *lockfile){
	strncpy(LOCKFILE,lockfile,256);
}

void set_workdir(const char *workdir){
	strncpy(WORKDIR,workdir,256);
}
